package com.leetcode.题库;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 覃国强
 * @date 2022-02-26 19:58
 */
public class 题库_0017_电话号码的字母组合 {

  static
  class Solution {
    static Map<Character, List<String>> map = new HashMap<>();
    static {
      map.put('2', new ArrayList<>(Arrays.asList("a", "b", "c")));
      map.put('3', new ArrayList<>(Arrays.asList("d", "e", "f")));
      map.put('4', new ArrayList<>(Arrays.asList("g", "h", "i")));
      map.put('5', new ArrayList<>(Arrays.asList("j", "k", "l")));
      map.put('6', new ArrayList<>(Arrays.asList("m", "n", "o")));
      map.put('7', new ArrayList<>(Arrays.asList("p", "q", "r", "s")));
      map.put('8', new ArrayList<>(Arrays.asList("t", "u", "v")));
      map.put('9', new ArrayList<>(Arrays.asList("w", "x", "y", "z")));
    }

    public List<String> letterCombinations(String digits) {
      return letterCombinations(digits, 0);
    }

    public List<String> letterCombinations(String digits, int index) {
      if (index == digits.length()) {
        return new ArrayList<>();
      }

      char num = digits.charAt(index);
      List<String> list = map.get(num);
      List<String> endList = letterCombinations(digits, index + 1);
      if (endList.isEmpty()) {
        return list;
      } else {
        List<String> result = new ArrayList<>();
        for (String start : list) {
          for (String end : endList) {
            result.add(start + end);
          }
        }
        return result;
      }
    }
  }

}
